package com.zmh.license.info;

import cn.hutool.core.collection.ListUtil;
import com.zmh.license.model.LicenseCheckModel;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.List;

/**
 * @author summer
 * @date 2023-03-13 11:02
 */
@Slf4j
public abstract class LicenseAbstractServer {


    public LicenseCheckModel getServerInfos(){
        LicenseCheckModel result = new LicenseCheckModel();
        try {
            result.setIpAddress(this.getIpAddress());
            result.setMacAddress(this.getMacAddress());
            result.setCpuSerial(this.getCpuInfo());
            result.setMainBoardSerial(this.getMainBoardSerial());
        }catch (Exception e){
            log.error("获取服务器硬件信息失败",e);
        }
        return result;
    }


    /**
     * 获取ip地址
     * @return
     */
    protected abstract List<String> getIpAddress();

    /**
     * 获取mac地址
     * @return
     */
    protected abstract List<String> getMacAddress();

    /**
     * 获取CPU
     * @return
     */
    protected abstract String getCpuInfo() throws IOException;

    /**
     * 获取串口信息
     * @return
     */
    protected abstract String getMainBoardSerial() throws IOException;


    protected List<InetAddress> getLocalAllInetAddress(){
        List<InetAddress> lists= ListUtil.list(true);
        try {
            for (Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {
                NetworkInterface iface = networkInterfaces.nextElement();
                // 在所有的接口下再遍历IP
                for (Enumeration<InetAddress> inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
                    InetAddress inetAddr = inetAddresses.nextElement();
                    if(!inetAddr.isLoopbackAddress()
                            && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()){
                        lists.add(inetAddr);
                    }
                }
            }
        }catch (Exception e){

        }
        return lists;
    }

    /**
     * 获取某个网络接口的Mac地址
     * @param inetAddr
     * @return
     */
    protected String getMacByInetAddress(InetAddress inetAddr){
        try {
            byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress();
            StringBuffer stringBuffer = new StringBuffer();

            for(int i=0;i<mac.length;i++){
                if(i != 0) {
                    stringBuffer.append("-");
                }

                //将十六进制byte转化为字符串
                String temp = Integer.toHexString(mac[i] & 0xff);
                if(temp.length() == 1){
                    stringBuffer.append("0" + temp);
                }else{
                    stringBuffer.append(temp);
                }
            }

            return stringBuffer.toString().toUpperCase();
        } catch (SocketException e) {
            log.error(e.getMessage());
        }
        return null;
    }
}
